package com.gxa.ssm340.controller;

import com.gxa.ssm340.entity.dto.CpnDTO;
import com.gxa.ssm340.entity.pojo.Coupon1;
import com.gxa.ssm340.service.CouponService;
import io.swagger.annotations.Api;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 导出控制器
 */
@Api(value = "优惠券导出",tags = "导出优惠券列表操作接口")
@RestController
@RequestMapping("/cpn")
public class ExportController {
    @Autowired
    private CouponService couponService;

    @GetMapping("/exportCpnList")
    public void exportData(HttpServletResponse response) throws IOException {
        // 创建一个工作簿对象
        Workbook workbook = new XSSFWorkbook();
        CpnDTO cpnDTO = new CpnDTO();
        cpnDTO.setPage(1);
        cpnDTO.setLimit(100);
        List<Coupon1> coupons = couponService.selectCouponAll(cpnDTO);
        // 创建一个工作表对象
        Sheet sheet = workbook.createSheet("Data");

        // 创建行和单元格，并将数据写入单元格中
        int rowIndex = 0;
        int cellIndex = 0;
        Row row0 = sheet.createRow(rowIndex++);
        row0.createCell(cellIndex++).setCellValue("编号");
        row0.createCell(cellIndex++).setCellValue("优惠券名称");
        row0.createCell(cellIndex++).setCellValue("规则");
        row0.createCell(cellIndex++).setCellValue("图片路径");
        row0.createCell(cellIndex++).setCellValue("描述");
        row0.createCell(cellIndex++).setCellValue("最后领取日期");
        row0.createCell(cellIndex++).setCellValue("最后使用日期");
        for (Coupon1 item : coupons) {
            Row row = sheet.createRow(rowIndex++);
            cellIndex = 0;
            row.createCell(cellIndex++).setCellValue(item.getId());
            row.createCell(cellIndex++).setCellValue(item.getName());
            row.createCell(cellIndex++).setCellValue(item.getDesc());
            row.createCell(cellIndex++).setCellValue(item.getImg());
            row.createCell(cellIndex++).setCellValue(item.getRole());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (item.getCollectExp()!=null&&item.getUsedExp()!=null) {
                String format = simpleDateFormat.format(item.getCollectExp());
                String format1 = simpleDateFormat.format(item.getUsedExp());
                row.createCell(cellIndex++).setCellValue(format);
                row.createCell(cellIndex++).setCellValue(format1);
            }
        }
// 自动调整所有列的单元格大小
        for (int i = 0; i < coupons.get(0).getClass().getDeclaredFields().length; i++) {
            sheet.autoSizeColumn(i);
        }
        // 设置响应头信息，告诉浏览器响应的是一个Excel文件
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;");

        // 将Excel文件写入响应输出流中，返回给前端
        workbook.write(response.getOutputStream());
    }
}

